#include "stdafx.h"

#include "RecordTable.h"

QRecordTable::QRecordTable(QObject *parent)
	: QRecord(parent)
{

}

QRecordTable::~QRecordTable()
{

}

/***********************************************************************************************************************************/
int QRecordTable::showColCount() const
{
	return listShowCols.count();
}

int QRecordTable::getActualCol(int showCol)
{
	CHK_EXP_RET(showCol < 0 || showCol >= colCount(), -1);
	return listShowCols.at(showCol);
}

QVariant QRecordTable::showData(int row, int col) const
{
	CHK_EXP_RET(col < 0 || col >= showColCount(), QVariant());

	int showCol = listShowCols.at(col);
	return data(row, showCol);
}

COLUMN_FLAGS QRecordTable::showFlags(int col) const
{
	CHK_EXP_RET(col < 0 || col >= showColCount(), CF_NONE);

	short showCol = listShowCols.at(col);
    CHK_EXP_RET(showCol < 0 || showCol >= listColumns.count(), CF_NONE);

    return listColumns.at(showCol).Flag;
}

QList<short> & QRecordTable::showRecords() const
{
	return listShowCols;
}

QMap< int, ROW_EDIT_TYPES > & QRecordTable::editRecords() const
{
	return editRows;
}

/***********************************************************************************************************************************/
void QRecordTable::setCol(int col, COLUMN_FLAGS flag, const QString & alias)
{
	CHK_EXP_END(col < 0 || col >= colCount());

	listColumns[col].Flag = flag;
	listColumns[col].Alias = alias;

	if (flag & CF_SHOW)
	{
		int index = listShowCols.indexOf(col);
		CHK_EXP_RUN(index < 0, listShowCols.append(col));
	}

}

void QRecordTable::setCol(const QString & name, COLUMN_FLAGS flag, const QString & alias)
{
	int col = getCol(name);
	setCol(col, flag, alias);
}

void QRecordTable::addCol(const QString & name, COLUMN_TYPE type, COLUMN_FLAGS flag, const QString & alias)
{
	wipeData();
	int count = listColumns.count();
	CHK_EXP_RUN(flag & CF_SHOW, listShowCols.append(count));

	ColumnInfo Info;
    Info.Name = name;
	Info.Flag = flag;
	Info.Alias = alias;
	Info.Type = type;
	listColumns.append(Info);
}

bool QRecordTable::setAt(int row, int col, const QVariant & value)
{
	CHK_EXP_RET(row < 0 || row >= rowCount(), false);
	CHK_EXP_RET(col < 0 || col >= colCount(), false);
	listRows[row][col] = value;
	return true;
}

//================================================================================================================================
int QRecordTable::insertRow(QVariantList & values, bool bSeq)
{
	listRows.append(values);
	return 1;
}

bool QRecordTable::modifyShow(int row, int col, const QVariant & value)
{
	CHK_EXP_RET(!editManger(row, RET_MODIFY), false);
	CHK_EXP_RET(col >= listShowCols.count() || col < 0, false);
	short showCol = listShowCols.at(col);
	CHK_EXP_RET(showCol >= listColumns.count(), false);
	CHK_EXP_RET(!(listColumns.at(showCol).Flag & CF_EDIT), false);
	return setAt(row, showCol, value);
}

bool QRecordTable::deleteRow(int row)
{
	listRows.removeAt(row);
	return true;
	//return editManger(row, RET_DELETE);
}

bool QRecordTable::removeRow(int row)
{
	return true;
}

void QRecordTable::clearEditFlags(ROW_EDIT_TYPES Flags)
{
	if (Flags == RET_NONE)
	{
		editRows.clear();
		return;
	}

	QList< int > & Indexs = editRows.keys(Flags);
	for (int i = 0; i < Indexs.count(); ++i)
	{
		editRows.remove(Indexs.at(i));
	}
}

/***********************************************************************************************************************************/
void QRecordTable::clear()
{
	wipeData();
	listShowCols.clear();
	listColumns.clear();
}

//-----------------------------------------------------------------------------------------------------------------------
bool QRecordTable::editManger(int index, ROW_EDIT_TYPES Type)
{
	if (editRows.contains(index))
	{
		if (Type == RET_DELETE)
		{
			editRows[index] = RET_DELETE;
			return true;
		}

		ROW_EDIT_TYPES OldType = editRows.value(index);
		CHK_EXP_RET(OldType == Type, true);
		CHK_EXP_RET(Type == RET_INSERT, false);
		CHK_EXP_RET(OldType == RET_INSERT, true);
		CHK_EXP_RET(OldType == RET_DELETE, false);
	}
	else
	{
		CHK_EXP_RET(primaryKeys.count() == 0 && Type == RET_INSERT, false);
		editRows.insert(index, Type);
		return true;
	}
	return false;
}
